In [1]:
    
%%html
<style>
table {float:left}
</style>
    
    
In this notebook, we will be covering the basics of using the pyhpimc python module to access the RESTFUL interface ( eAPI ) of the HPE IMC Network Management Server.
The python library is currently available at HPE Github repository
In [7]:
    
import csv
import time
from pyhpeimc.auth import *
from pyhpeimc.plat.groups import *
from pyhpeimc.version import *
    
In [8]:
    
2+34
    
    Out[8]:
In [10]:
    
auth = IMCAuth("http://", "10.101.0.203", "8080", "admin", "admin")
    
In [11]:
    
def print_views():
    views_list = get_custom_views(url=auth.url, auth=auth.creds)
    print ("There are a total of " + str(len(views_list)) + " views currently")
    for view in views_list:
        print (view['name'])
    print (json.dumps(views_list[0], indent = 4))
    
In [12]:
    
print_views()
    
    
In this step, we will create two custom views Canada and Alberta using the create_custom_views() function.
For this example, we will create
| Name | Upperview | 
|---|---|
| Canada | |
| Alberta | Canada | 
| Calgary | Alberta | 
In [ ]:
    
    
In [25]:
    
create_custom_views(auth=auth.creds, url=auth.url, name="Canada")
create_custom_views(auth=auth.creds, url=auth.url, name="Alberta",upperview='Canada')
create_custom_views(auth=auth.creds, url=auth.url, name="Calgary",upperview='Alberta')
print_views()
    
    
In [8]:
    
get_custom_views(url=auth.url, auth=auth.creds, name="Canada")
    
    Out[8]:
In [9]:
    
get_custom_views(url=auth.url, auth=auth.creds, name="Alberta")
    
    Out[9]:
In [10]:
    
get_custom_views(url=auth.url, auth=auth.creds, name="Calgary")
    
    Out[10]:
In [26]:
    
delete_custom_view(url=auth.url, auth=auth.creds, name='Canada')
delete_custom_view(url=auth.url, auth=auth.creds, name='Alberta')
delete_custom_view(url=auth.url, auth=auth.creds, name='Calgary')
print_views()
    
    
In [4]:
    
with open('custom_views.csv') as f:
    s = f.read()
    print (s)
    
    
In [14]:
    
def import_custom_views(filename):
    """
    Function which takes in a csv files as input to the create_custom_views function from the pyhpimc python module
    available at https://github.com/HPNetworking/HP-Intelligent-Management-Center
    :param filename: user-defined filename which contains two column named "name" and "upperview" as input into the
    create_custom_views function from the pyhpimc module.
    :return: returns output of the create_custom_vies function (str) for each item in the CSV file.
    """
    with open(filename) as csvfile:
        # decodes file as csv as a python dictionary
        reader = csv.DictReader(csvfile)
        for view in reader:
            # loads each row of the CSV as a JSON string
            name = view['name']
            upperview = view['upperview']
            if len(upperview) is 0:
                upperview = None
            create_custom_views(auth=auth.creds, url=auth.url,name=name,upperview=upperview)
    
In [15]:
    
start_time = time.time()
import_custom_views('custom_views.csv')
print("--- %s seconds ---" % (time.time() - start_time))
    
    
In [16]:
    
views_list = get_custom_views(url=auth.url, auth=auth.creds)
print_views()
print ("There are a total of " + str(len(views_list)) + " views currently")
    
    
In [17]:
    
def delete_custom_views_csv(filename):
    """
    Function which takes in a csv files as input to the delete_custom_view function from the pyhpeimc python module
    available at https://github.com/HPENetworking/HP-Intelligent-Management-Center
    :param filename: user-defined filename which contains two column named "name" and "upperview" as input into the
    create_custom_views function from the pyhpimc module.
    :return: returns output of the create_custom_vies function (str) for each item in the CSV file.
    """
    with open(filename) as csvfile:
        # decodes file as csv as a python dictionary
        reader = csv.DictReader(csvfile)
        for view in reader:
            # loads each row of the CSV as a JSON string
            name = view['name']
            delete_custom_view(url=auth.url, auth=auth.creds, name=name)
    
In [18]:
    
start_time = time.time()
delete_custom_views_csv('custom_views.csv')
print("--- %s seconds ---" % (time.time() - start_time))
    
    
In [ ]: